<!DOCTYPE html>
<html>

<?php 

require_once("DB_Config.php");
require_once("func.php"); //Access To session_start
$db_connect = DB_Connection::getInstance();


?>

<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<link href="css/style.css" rel='stylesheet' type='text/css' />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/jquery.package-slider.js"></script>
<script type="text/javascript" src="js/jquery.dropdown.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"></script>
<title>A&A ONLINE HOUSE SHOWER</title>
</head>

<script type="text/javascript">

function appar_size() {
    var e = document.getElementById("rsize");
    if ( isNaN(e.value) || (e.value > 600) || (e.value < 0) ) {
        alert("invalid number, please enter numbet between 0-600");
        e.value="";
    }
}


//chaecking that the minimum price range is correct
function p_range_min() {
    var e = document.getElementById("prangemin");
    if ( isNaN(e.value) || (e.value < 99) || (e.value > 50000000) ) {
        alert("invalid price , please enter price between 100 - 50000000");
        e.value="";
    }
    else{
        var x =  e.value;
    }
}

$(function() { $( "#datetab" ).datepicker( {  maxDate: "+0D" ,dateFormat: 'yy-mm-dd'} );  } );

//case district been changed and need to change to city select

function cit() {   
    var e = document.getElementById("district");
    var dis_name = e.options[e.selectedIndex].value;
    var flag=1;  //recognaizing in data.php who called him
     $('#city').load('data.php?dis_name=' + escape(dis_name) + '&flag=' + escape(flag)) ;
}


function googlego(){

  var district = document.getElementById("district");
  var city = document.getElementById("city");
  var apparstr = document.getElementById("apparstr");
  
  var flag=4;  
    if ( !(district.value == '') && !(city.value == '') && !(apparstr.value == '') ) {

      var district = document.getElementById("district");
      var city = city.value;
      var apparstr = apparstr.value;
      
     $.get('data.php?city=' + escape(city) + '&apparstr=' + escape(apparstr) + '&flag=' + escape(flag), function(data) {
     $('#cordinates').val(data);
      });
    }
  }

$(document).ajaxComplete(function() {

  initialize();

});


function initialize(){
  var cor = document.getElementById("cordinates").value;
  var n=cor.split(",");
  if (n[0]!="xxxxxxxxxx" && n[0]!=""){
    var myCenter=new google.maps.LatLng(n[0],n[1]);
    var marker;
    var mapProp = { center:new google.maps.LatLng(n[0],n[1]), zoom:17, mapTypeId:google.maps.MapTypeId.ROADMAP };
    var map=new google.maps.Map(document.getElementById("map_show"),mapProp);
    marker=new google.maps.Marker({ position:myCenter, animation:google.maps.Animation.BOUNCE });
    marker.setMap(map);
    google.maps.event.trigger(map, 'resize');
    map.setZoom( map.getZoom() ); 
    map.setCenter(marker.getPosition());
  }  

}
  var youtube_ver1 = /http:\/\/youtu.be\/[a-zA-Z0-9]+/; 
  var youtube_ver2 = /www.youtube.com\/watch\?[a-zA-Z0-9\\=]+/; 
  var youtube_ver3 = /www.youtube.com\/embed\/[a-zA-Z0-9]+/; 
  function check_youtube() {

      var e = document.getElementById("youtube");
      if ( !e.value.match(youtube_ver1) && !e.value.match(youtube_ver2) && !e.value.match(youtube_ver3) ){ document.getElementById('youtube_error').innerHTML="Error In The YouTube URL"; }
      else{ document.getElementById('youtube_error').innerHTML="&nbsp"; }
  }
 
</script>


  <body>
    <!-- START HEADER WRAPER-->
    <div id="header-color">
      <div id="header-wrapper">
        <?php require_once("header.php"); 
          if(isset($_SESSION['myusername'])==FALSE)  { header("location:index.php");} 
          else {?> <b class="hellouser"> Hello <?php echo $_SESSION['myusername']; ?></b> <?php } ?>
      </div>
    </div>

<!-- START CONTENT WRAPER-->
<div id="page-wrapper">

  <!--
__________              .__         __                 __  .__                ___________                 
\______   \ ____   ____ |__| ______/  |_____________ _/  |_|__| ____   ____   \_   ________________  _____  
 |       __/ __ \ / ___\|  |/  ___\   __\_  __ \__  \\   __|  |/  _ \ /    \   |    __)/  _ \_  __ \/     \ 
 |    |   \  ___// /_/  |  |\___ \ |  |  |  | \// __ \|  | |  (  <_> |   |  \  |     \(  <_> |  | \|  Y Y  \
 |____|_  /\___  \___  /|__/____  >|__|  |__|  (____  |__| |__|\____/|___|  /  \___  / \____/|__|  |__|_|  /
        \/     \/_____/         \/                  \/                    \/       \/                    \/ 
  -->  



<div id="header-wrapper">
<form name="rent_registration_form" method="post" action="" style=" height: 490px; ">

    <table class="regtlb" dir="ltr">
    <i style="margin-left: 222px;">Please Insert Your Property Properties</i>
    </table>
    
    <div class="subjects"><!-- START TABLE -->

        <table class="regtlb" dir="ltr">
            <tr>
                <td class="show_header"><b class="show_text">APARTMENT PROPERTIES</b></td>
            </tr>
        </table>


          <td width="242" valign="top"><!-- APARTMENT PROPERTIES -->
              <table width="245" border="0" align="left" cellpadding="0" cellspacing="0">
                <tr>        
                  <td width="65" style="padding-right: 10px">District:</td>


                    <td> <!-- APARTMENT DISTRICT -->
                         <select id="district" name="district" style="width: 100px;" onchange="cit()">
                           <option value="0"> </option>
                            <?php
                                        $db_connect = DB_Connection::getInstance();
                                        $q = 'SELECT DISTINCT district  FROM cities';
                                        $stmt = $db_connect->db_conn->prepare($q);
                                        if ($stmt->execute()){
                                             $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
                                             $i = 0; 
                                             while ($i < count($res)) {                     
                                ?>
                                
                            <option value= "<?php  echo $res[$i]['district']; ?>" >
                                            <?php  echo $res[$i]['district']; $i++; ?> 
                            </option>
                            <?php  } } ?>
                        </select>
                  </td>
                </tr>  <!-- APARTMENT DISTRICT END -->  

                <tr>    <!-- APARTMENT CITY --> 
                  <td width="65" style="padding-right: 10px">City:</td>
                      <td>
                        <select id ="city" name="city" style="width: 130px;" onchange="googlego()" >
                            <option selected="selected" value=""></option>
                        </select>
                    </td>
                </tr>        

                 <tr><!-- APARTMENT ROOMS --> 
                      <td width="65" style="padding-right: 10px"># of Rooms:</td>
                      <td>
                        <select id ="min_num_room" name="min_num_room" style="width: 55px;" onchange="">
                            <option value= "1"   >1   </option>
                            <option value= "1.5" >1.5 </option>
                            <option value= "2"   >2   </option>
                            <option value= "2.5" >2.5 </option>
                            <option value= "3"   >3   </option>
                            <option value= "3.5" >3.5 </option>
                            <option value= "4"   >4   </option>
                            <option value= "4.5" >4.5 </option>
                            <option value= "5"   >5   </option>
                            <option value= "5.5" >5.5 </option>
                            <option value= "6"   >6   </option>
                            <option value= "6.5" >6.5 </option>
                            <option value= "7"   >7   </option>
                            <option value= "7.5" >7.5 </option>
                            <option value= "8"   >8   </option>
                            <option value= "8.5" >8.5 </option>
                            <option value= "9"   >9   </option>
                            <option value= "9.5" >9.5 </option>
                            <option value= "10"  >10  </option>
                            <option value= "10.5">10.5</option>
                            <option value= "11"  >11  </option>
                            <option value= "11.5">11.5</option>
                            <option value= "12"  >12  </option>
                        </select>
                    </td>                                                                                                          
                                                                             
                </tr>

                <tr><!-- APARTMENT FLOORS --> 
                      <td width="65" style="padding-right: 10px">Floor #:</td>            
                        <td>
                            <select id ="minfloor" name="minfloor" style="width: 55px;" onchange="">
                                <option value="0">0</option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                                <option value="6">6</option>
                                <option value="7">7</option>
                                <option value="8">8</option>
                                <option value="9">9</option>
                                <option value="10">10</option>
                                <option value="11">11</option>
                                <option value="12">12</option>
                                <option value="13">13</option>
                                <option value="14">14</option>
                                <option value="15">15</option>
                                <option value="16">16</option>
                                <option value="17">17</option>
                                <option value="18">18</option>
                                <option value="19">19</option>
                                <option value="100">20+</option>
                            </select>
                        </td>
                     </tr>

                <tr><!-- APARTMENT SQUARE --> 
                  <td width="69" style="padding-right: 10px">Square&nbspFoot:</td>            
                    <td>
                     <input type="text" name="rsize" id="rsize" value="" style="width: 25px;" onchange="appar_size()">
                    </td>
                </tr>


                <tr><!-- APARTMENT PRICE --> 
                  <td width="69" style="padding-right: 10px">Price Range:</td>            
                    <td>
                     <input type="text" name="prangemin" id="prangemin" value="" style="width: 60px;" onchange="p_range_min()">
                    </td>
                </tr>

                <tr><!-- PROPERTY TYPE --> 
                      <td width="63" style="padding-right: 10px">Type:</td>
                        <td>
                            <select id ="type" name="type" style="width: 80px;">
                            <option selected="selected" value="sellhouse">Sell</option>
                            <option value="renthouse">Rent</option>
                            <option value="partnerhouse">Rent(Partners)</option>
                            </select>
                        </td>
                    </tr>


                    <tr><!-- APARTMENT PUROSE --> 
                      <td width="63" style="padding-right: 10px">Purpose:</td>
                        <td>
                            <select id ="purpose" name="purpose" style="width: 80px;">
                            <option selected="selected" value="sellhouse">Sell</option>
                            <option value="apartment">Apartment</option>>
                            <option value="studio">Studio</option>
                            <option value="penthouse">Penthouse</option>
                            <option value="basement">Basement</option>
                            <option value="duplex">Duplex</option>
                            <option value="triplex">Triplex</option>
                            <option value="housing_unit">Housing unit</option>
                            <option value="warehouse">Warehouse</option>
                            <option value="residential_building">Residential building</option>
                            <option value="lot">Lot</option>
                            </select>
                        </td>
                    </tr> 

                    




              </table>
            </td>    
        </div>


        <!--

\_   ___ \  ____ |  |  __ __  _____   ____        \_____  \ 
/    \  \/ /  _ \|  | |  |  \/     \ /    \        /  ____/ 
\     \___(  <_> |  |_|  |  |  Y Y  |   |  \      /       \ 
 \______  /\____/|____|____/|__|_|  |___|  /      \_______ \
        \/                        \/     \/               \/

         -->  


    <div class="subjects"><!-- START TABLE -->

        <table class="regtlb" dir="ltr">
            <tr>
                <td class="show_header"><b class="show_text">APARTMENT PROPERTIES</b></td>
            </tr>
        </table>

          <td width="242" valign="top">
              <table width="245" border="0" align="left" cellpadding="0" cellspacing="0">

                <tr>        
                  <td width="120px" style="padding-right: 10px">Enter Date:</td>
                    <td> <!-- APARTMENT DATE -->
                        <input type="text" name="datetab" id="datetab" value="" style="width: 80px;" onchange="">
                    </td>
                </tr>   

                <tr>        
                  <td width="100" style="padding-right: 10px">Street Name:</td>
                    <td> <!-- APARTMENT STREET -->
                        <input type="text" name="apparstr" id="apparstr" value="" style="width: 80px;" onchange="googlego()">
                    </td>
                </tr>    

                <tr>    <!-- APARTMENT Warehouse --> 
                  <td width="65" style="padding-right: 10px">Warehouse:</td>
                      <td>
                        <input type="checkbox" name="Warehouse" value="1">
                    </td>
                </tr>

                <tr>    <!-- APARTMENT Parking --> 
                  <td width="65" style="padding-right: 10px">Parking:</td>
                      <td>
                        <input type="checkbox" name="Parking" value="1">
                    </td>
                </tr>  

                 <tr>    <!-- APARTMENT Parking --> 
                  <td width="75" style="padding-right: 10px">Air Conditioner:</td>
                      <td>
                        <input type="checkbox" name="air_conditioner" value="1">
                    </td>
                </tr>  

                 <tr>    <!-- APARTMENT Elevator --> 
                  <td width="65" style="padding-right: 10px">Elevator:</td>
                      <td>
                        <input type="checkbox" name="Elevator" value="1">
                    </td>
                </tr> 
                 <tr>    <!-- APARTMENT Comments --> 
                  <td width="65" style="padding-right: 10px">Comments:</td>
                      <td>
                       <input type="text" name="Comments" value="Your Comment Here">
                    </td>
                </tr> 
                <tr>    <!-- APARTMENT Comments --> 
                  <td width="65" style="padding-right: 10px">Cordinats:</td>
                      <td>
                       <input type="text" name="cordinates" id="cordinates" value="xxxxxxxxxx, xxxxxxxxx">
                    </td>
                </tr>
              </table> <!-- COLUMNS END -->
            </td>    
        </div>

<div class="subjects"><!-- GOOGLE MAP -->
  <div margin="0px 0px 0px 0px">
  <table class="regtlb" dir="ltr">
  <tr><td class="show_header"><b class="show_text">GOOGLE MAP</b></td></tr>
 </table>

<!-- Google Maps -->
          <td width="242" valign="top">
              <table  class="table_show" width="242" border="0" align="right" cellpadding="0" cellspacing="0">
                <tr>
                  <td colspan="3">
                    <div id="andrei" value="first"></div>
                    <div id="map_show" style="width:385px;height:180px;">
                    </div>         
                </td> 
              </tr>
          </table>
        </td>   
      </div>
    </div><!-- GOOGLE MAP END -->
<table class="regtlb" dir="ltr" width="800" border="0"style="margin-left: -255px;" align="left" cellpadding="0" cellspacing="0">
  <tr>    <!-- youtube link Comments --> 
    <td width="88" style="padding-right: 10px">Youtube link:</td>
    <td width="350"><input type="text" name="youtube" style="margin-left:-2px; width:350px;" id="youtube" value="" onchange="check_youtube()"></td>
    <td width="400"><div id="youtube_error"></div></td>
  </tr>
</table>
<td><input name="Submit" type="submit" value="Submit" class="button" /></td>

</form>


          </div><!-- END HEADER WRAPER-->
        
      </div><!-- END PAGE WRAPER-->
    

<!--

  _________      .__    ________                            .__               
 /   _____/ _____|  |   \_____  \  __ __  __________________|__| ____   ______
 \_____  \ / ____|  |    /  / \  \|  |  _/ __ \_  __ \_  __ |  _/ __ \ /  ___/
 /        < <_|  |  |__ /   \_/.  |  |  \  ___/|  | \/|  | \|  \  ___/ \___ \ 
/_______  /\__   |____/ \_____\ \_|____/ \___  |__|   |__|  |__|\___  /____  >
        \/    |__|             \__>          \/                     \/     \/ 


-->

<?php

if( isset($_POST['Submit']) ){

  if( isset($_POST['adid']) ){//delete the ad before the changes 
                              // gets adid from edit_remove_apart.php
    $adkind = $_POST['oldtype'];
    print "$adkind";
    $ad___id = $_POST['adid'];
    echo "$ad___id";
    if ($adkind == "partnerhouse"){ $sql="DELETE FROM partnerhouse WHERE ad_id=?";}
    if ($adkind == "renthouse")   { $sql="DELETE FROM renthouse    WHERE ad_id=?";}
    if ($adkind == "sellhouse")   { $sql="DELETE FROM sellhouse    WHERE ad_id=?";}
    $stmt = $db_connect->db_conn->prepare($sql);
    $param = array($_POST['adid']);
    if ($stmt->execute($param)){
      echo" have been deleted<br>";
    }
    else{
      echo "&nbspuser havent been deleted - ERROR<br>";
    }


  }

  // echo $_POST['city'];
  // echo $_POST['apparstr'];
  // echo $_POST['rsize'];
  // echo $_POST['minfloor'];
  // echo $_POST['min_num_room'];
  // echo $_SESSION['myusername'];
  // echo $_POST['prangemin'];
  // echo $_POST['datetab'];
  // echo $_POST['Comments'];
  // echo $_POST['purpose'];

  //upload new/updated ad
  $c=$_POST['cordinates'];
  $c =explode(",", $c);
  $district=$_POST['district'];
  $city_name=$_POST['city'];
  $address=$_POST['apparstr'];
  $type=$_POST['type'];
  $square_feet=$_POST['rsize'];
  $floor = $_POST['minfloor'];
  $num_bedrooms = $_POST['min_num_room'];
  $price = $_POST['prangemin'];
  $username = $_SESSION['myusername'];
  $upload_date = $_POST['datetab']; //Fix To Get Todays Date
  $longitude = $c[0];
  $latitude = $c[1];
  $comments = $_POST['Comments'];
  $property = $_POST['purpose'];
  $bump_date = $_POST['datetab']; //Fix To Get Todays Date
  $entrance_date = $_POST['datetab'];
  if( isset($_POST['adid']) ) { $ad_id = $_POST['adid']; }
  else { $ad_id = rand(1, 1000); }
  
  

  $picture = "0";

  if (isset($_POST['Warehouse']))       { $warehouse = $_POST['Warehouse']; } 
  else { $warehouse = "0"; }
  if (isset($_POST['Parking']))         { $parking = $_POST['Parking']; } 
  else { $parking = "0"; }
  if (isset($_POST['Elevator']))        { $elevator = $_POST['Elevator']; } 
  else { $elevator = "0"; }
  if (isset($_POST['air_conditioner'])) { $air_conditioner = $_POST['air_conditioner']; } 
  else { $air_conditioner = "0"; }
  
  $youtube = $_POST['youtube'];
  $youtube_md5 = md5($youtube);
  $sql = "INSERT INTO youtube_table(y_tube_md5, y_tube_link) VALUES (?,?)";
  $stmt = $db_connect->db_conn->prepare($sql);
  $param=array($youtube_md5,$youtube);
  $stmt->execute($param);


  if($type == "sellhouse"){
  $sql = "INSERT INTO sellhouse(district, city_name, address, property, square_feet, floor, num_bedrooms, warehouse,
    parking, air_conditioner, elevator, upload_date, bump_date, price, username, entrance_date, longitude,
    latitude, comments, ad_id, video, picture)
              VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
          $stmt = $db_connect->db_conn->prepare($sql);
          $param=array($district,$city_name , $address, $property, $square_feet, $floor, $num_bedrooms, $warehouse,
            $parking, $air_conditioner, $elevator, $upload_date, $bump_date, $price, $username, $entrance_date, $longitude,
            $latitude, $comments, $ad_id, $youtube_md5, $picture);
  }

  if($type == "renthouse"){
  $sql = "INSERT INTO renthouse(district, city_name, address, property, square_feet, floor, num_bedrooms, warehouse,
    parking, air_conditioner, elevator, upload_date, bump_date, price, username, entrance_date, longitude,
    latitude, comments, ad_id, video, picture)
              VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
          $stmt = $db_connect->db_conn->prepare($sql);
          $param=array($district,$city_name , $address, $property, $square_feet, $floor, $num_bedrooms, $warehouse,
            $parking, $air_conditioner, $elevator, $upload_date, $bump_date, $price, $username, $entrance_date, $longitude,
            $latitude, $comments, $ad_id, $youtube_md5, $picture);
  }

  if($type == "partnerhouse"){
  $sql = "INSERT INTO partnerhouse(district, city_name, address, property, square_feet, floor, num_bedrooms, warehouse,
    parking, air_conditioner, elevator, upload_date, bump_date, price, username, entrance_date, longitude,
    latitude, comments, ad_id, video, picture)
              VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
          $stmt = $db_connect->db_conn->prepare($sql);
          $param=array($district,$city_name , $address, $property, $square_feet, $floor, $num_bedrooms, $warehouse,
            $parking, $air_conditioner, $elevator, $upload_date, $bump_date, $price, $username, $entrance_date, $longitude,
            $latitude, $comments, $ad_id, $youtube_md5, $picture);
  }

  if($stmt->execute($param)){
  header("location:edit_remove_apart.php");
  }



  }
?>
    <!-- START FOOTER WRAPER-->
    <p><?php require("footer.php"); ?></p>
  </body>
</html>

